<?php

namespace app\radar\model;

use app\BaseModel;


class RadarQuestionnaire extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_qn_questionnaire';


    protected static function init ()
    {
        //TODO:初始化内容
    }

    /**
     * @Purpose: 获取问卷信息
     *
     * @Method: POST
     *
     * @Author: zzf
     *
     * @Return: mixed 查询返回值（结果集对象）
     */
    public function getQuestionnaireInfo ( $user_id, $staff_id, $uniacid )
    {
        $info = self::where( [ [ 'uniacid', '=', $uniacid ], [ 'status', '=', 1 ] ] )
                    ->find();

        if ( !$info )
        {
            return $info;
        }

        $info = $info->toArray();

        $questions = RadarQuestion::where( [ [ 'status', '=', 1 ], [ 'naire_id', '=', $info[ 'id' ] ] ] )
                                 ->select()
                                 ->toArray();

        $answers = RadarAnswer::where( [ [ 'user_id', '=', $user_id ], [ 'staff_id', '=', $staff_id ] ]
        )
                             ->select()
                             ->toArray();

        foreach ( $questions as $index => $item ) {
            $questions[ $index ][ 'answer' ] = '';
            foreach ( $answers as $k => $v ) {
                if ( $v[ 'q_id' ] == $item[ 'id' ] ) {
                    $questions[ $index ][ 'answer' ] = $v[ 'answer' ];
                    break;
                }
            }
        }

        $info[ 'questions' ] = $questions;
        $info[ 'answers' ]  = $answers;

        return $info;
    }

}